package com.woniu.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.woniu.product.mapper.CategoryMapper;
import com.woniu.product.pojo.Category;
import com.woniu.product.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lydia
 * @since 2023-02-16
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Resource
    private CategoryMapper categoryMapper;

    @Override
    public List<Category> findParentProductByCategory(String parentId) {
        //查询所有的parentId
        QueryWrapper wrapper = new QueryWrapper();
        //查询条件
        wrapper.eq("parent_id",parentId);
        //获取该父类Id下所有的子分类
        List<Category> list = categoryMapper.selectList(wrapper);
        for (Category category : list) {
            //清理之前的条件，在之前条件的基础上新增子类查询条件
            wrapper.clear();
            //获取父类id里面它们各自包含的子类id，输出一个列表
            wrapper.eq("parent_id",category.getId());
            category.setChildren(categoryMapper.selectList(wrapper));
        }
        return list;
    }
}
